#include "rtc.h"
#include "pulpino.h"

void set_rtc(RTC_TypeDef *RTCx, RTC_CFG_Type *RTC_ConfigStruct)
{
    uint32_t TS0_temp = 0;
    uint32_t TS1_temp = 0;
    
    CHECK_PARAM(PARAM_RTC_ADDR(RTCx));
    CHECK_PARAM(PARAM_RTC_Sec_RATE  (RTC_ConfigStruct->Sec_rate));
    CHECK_PARAM(PARAM_RTC_Min_RATE  (RTC_ConfigStruct->Min_rate));    
    CHECK_PARAM(PARAM_RTC_Hour_RATE (RTC_ConfigStruct->Hour_rate));  
    CHECK_PARAM(PARAM_RTC_Year_RATE (RTC_ConfigStruct->Year_rate)); 
    CHECK_PARAM(PARAM_RTC_Mon_RATE  (RTC_ConfigStruct->Mon_rate)); 
    CHECK_PARAM(PARAM_RTC_Day_RATE  (RTC_ConfigStruct->Day_rate));

    /* set second value */
    TS0_temp  = (uint32_t)(RTC_ConfigStruct->Sec_rate);
    /* set minute value */
    TS0_temp |= (uint32_t)(RTC_ConfigStruct->Min_rate<<8); 
    /* set Hour value */
    TS0_temp |= (uint32_t)(RTC_ConfigStruct->Hour_rate<<16);
    /* set Week value */
    TS1_temp  = (uint32_t)(RTC_ConfigStruct->Week_rate);
    /* set Day value */
    TS1_temp |= (uint32_t)(RTC_ConfigStruct->Day_rate<<4);
    /* set Mon value */
    TS1_temp |= (uint32_t)(RTC_ConfigStruct->Mon_rate<<12);
    /* set Year value */
    TS1_temp |= (uint32_t)(RTC_ConfigStruct->Year_rate<<16);
    RTCx->CTRL = 0;
    RTCx->TS0  = TS0_temp;
    RTCx->TS1  = TS1_temp; 
    RTCx->CTRL = TSR_ENABLE_MASK;
	delay(100); //wait time set
}
    
void set_rtc_alm(RTC_TypeDef *RTCx, RTC_ACFG_Type *RTC_AlmCfg, RTC_CFG_Type *RTC_Alm_time)
{
    CHECK_PARAM(PARAM_RTC_ADDR(RTCx));
    CHECK_PARAM(PARAM_RTC_Sec_RATE  (RTC_Alm_time->Sec_rate));
    CHECK_PARAM(PARAM_RTC_Min_RATE  (RTC_Alm_time->Min_rate));    
    CHECK_PARAM(PARAM_RTC_Hour_RATE (RTC_Alm_time->Hour_rate));  
    CHECK_PARAM(PARAM_RTC_Year_RATE (RTC_Alm_time->Year_rate)); 
    CHECK_PARAM(PARAM_RTC_Mon_RATE  (RTC_Alm_time->Mon_rate)); 
    CHECK_PARAM(PARAM_RTC_Day_RATE  (RTC_Alm_time->Day_rate));
    
    uint32_t ALARM_temp = 0;
    ALARM_temp  = (uint32_t)(RTC_AlmCfg->Ensec);
    ALARM_temp |= (uint32_t)(RTC_AlmCfg->Enmin<<1);
    ALARM_temp |= (uint32_t)(RTC_AlmCfg->Enhour<<2);
    ALARM_temp |= (uint32_t)(RTC_AlmCfg->Enweek<<3);
    ALARM_temp |= (uint32_t)(RTC_AlmCfg->Enday<<4);
    ALARM_temp |= (uint32_t)(RTC_AlmCfg->Enmon<<5);
    ALARM_temp |= (uint32_t)(RTC_AlmCfg->Enyear<<6);
    ALARM_temp |= (uint32_t)(RTC_AlmCfg->Enset<<8);
    RTCx->ACTRL = ALARM_temp;
    
    uint32_t ALARM_Hour = 0;
    ALARM_Hour  = (uint32_t)(RTC_Alm_time->Sec_rate);
    ALARM_Hour |= (uint32_t)(RTC_Alm_time->Min_rate<<8);
    ALARM_Hour |= (uint32_t)(RTC_Alm_time->Hour_rate<<16);
    RTCx->AS0 = ALARM_Hour;
    
    uint32_t ALARM_Day = 0;
    ALARM_Day = RTC_Alm_time->Week_rate;
    ALARM_Day |= RTC_Alm_time->Day_rate<<4;
    ALARM_Day |= RTC_Alm_time->Mon_rate<<12;
    ALARM_Day |= RTC_Alm_time->Year_rate<<16;
    RTCx->AS1 = ALARM_Day;

}

uint16_t get_year(RTC_TypeDef *RTCx)
{
    CHECK_PARAM(PARAM_RTC_ADDR(RTCx));
    
    uint8_t year_temp = RTCx->TIM1 >> 16;
    uint16_t year = year_temp + 2000;
    return year;
}

uint8_t get_month(RTC_TypeDef *RTCx)
{
    CHECK_PARAM(PARAM_RTC_ADDR(RTCx));
    
    RTCx->CTRL = 0x04;
    uint8_t month = (RTCx->TIM1 >> 12) & 0x0F;
    return month;
}

uint8_t get_day(RTC_TypeDef *RTCx)
{
    CHECK_PARAM(PARAM_RTC_ADDR(RTCx));
    
    RTCx->CTRL = 0x04;
    uint8_t day = (RTCx->TIM1 >> 4) & 0x1F;
    return day;
}

uint8_t get_week(RTC_TypeDef *RTCx)
{
    CHECK_PARAM(PARAM_RTC_ADDR(RTCx));
    
    RTCx->CTRL = 0x04;
    uint8_t weeek = RTCx->TIM1 & 0x07;
    return weeek;
}

uint8_t get_hour(RTC_TypeDef *RTCx)
{
    CHECK_PARAM(PARAM_RTC_ADDR(RTCx));
    
    RTCx->CTRL = 0x04;
    uint8_t hour = (RTCx->TIM0 >> 16) & 0x1F;
    return hour;
}

uint8_t get_min(RTC_TypeDef *RTCx)
{
    CHECK_PARAM(PARAM_RTC_ADDR(RTCx));
    
    RTCx->CTRL = 0x04;
    uint8_t min = (RTCx->TIM0 >> 8) & 0x3F;
    return min;
}

uint8_t get_sec(RTC_TypeDef *RTCx)
{
    CHECK_PARAM(PARAM_RTC_ADDR(RTCx));
    
    RTCx->CTRL = 0x04;
    uint8_t sec = RTCx->TIM0 & 0x3F;
    return sec;
}

void get_now_time(RTC_TypeDef *RTCx, RTC_CFG_Type *RTC_ConfigStruct)
{
    CHECK_PARAM(PARAM_RTC_ADDR(RTCx));
    
    RTCx->CTRL = 0x04;
    RTC_ConfigStruct->Year_rate = (RTCx->TIM1 >> 16) + 2000;
    RTC_ConfigStruct->Mon_rate  = (RTCx->TIM1 >> 12) & 0x0F;
    RTC_ConfigStruct->Day_rate  = (RTCx->TIM1 >>  4) & 0x1F;
    RTC_ConfigStruct->Hour_rate = (RTCx->TIM0 >> 16) & 0x1F;
    RTC_ConfigStruct->Min_rate  = (RTCx->TIM0 >>  8) & 0x3F;
    RTC_ConfigStruct->Sec_rate  = RTCx->TIM0 & 0x3F;    
}